﻿#region Using namespaces

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AvalonDock;
using GraphSharp;
using GraphSharp.Controls;
using BOL.Apps.GeneNetworkBrowser;
using BOL.Apps.GeneNetworkBrowser.ViewModels;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.Controls
{
    public partial class SubNetworksDocument : DocumentContent
    {
        #region Public properties

        public SubNetworksDocumentViewModel ViewModel { get { return DataContext as SubNetworksDocumentViewModel; } }

        #endregion

        #region Constructor

        public SubNetworksDocument()
        {
            InitializeComponent();
        }  

        #endregion

        private void ApplyCox(object sender, RoutedEventArgs e)
        {
            ViewModel.ApplyCox();

            var genes = new HashSet<Gene>();

            foreach (var subNetwork in ViewModel.SubNetworks)
                foreach (var gene in subNetwork.Vertices)
                    genes.Add(gene);

            var min = genes.Min(g => (double)g.Attributes["Cox"]);
            var max = genes.Max(g => (double)g.Attributes["Cox"]);
            var denominator = max - min;

            VertexControl vc;
            var networkGraphLayout = GetVisualChild<NetworkGraphLayout>(lvNetwork); 

                //gvcNetwork.CellTemplate.FindName("_networkGraphLayout", lvNetwork) as NetworkGraphLayout;

            var minColor = System.Drawing.Color.FromName("Red");
            var maxColor = System.Drawing.Color.FromName("Green");
            var minH = (double)minColor.GetHue() / 360.0;
            var maxH = (double)maxColor.GetHue() / 360.0;

            foreach (var gene in genes)
            {
                vc = networkGraphLayout.GetVertexControl(gene);

                //if (vc != null)
                //{
                    var value = ((double)gene.Attributes["Cox"] - min) / denominator;

                    double h = (value < 0.5) ? minH : maxH;
                    double s = (value < 0.5) ? (0.5 - value) * 2 : (value - 0.5) * 2;
                    double l = 0.5;

                    vc.Background = new SolidColorBrush(Converters.ColorSystems.FromHSL(h, s, l));
                //}
            }
        }

        private void ApplyExpression(object sender, RoutedEventArgs e)
        {

        }

        private T GetVisualChild<T>(DependencyObject parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        } 
    }
}
